package com.woniuxy.stage3demo.mapper.chenyuxuanMapper;

import com.woniuxy.stage3demo.bean.chenyuxuanBean.OptLogBean;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.StaffBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

@Mapper
public interface OptLogMapper {
    /**
     * 添加操作日志
     *
     * @return
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "o.id"
    )
    @Insert("insert into opt_log (opt_log_no,log_time,ip,log_content,stuff_id,del_flag)" +
            "values(#{o.optLogNo},#{o.logTime},#{o.ip},#{o.logContent},#{s.id},#{o.delFlag})")
    int addOptLog(@Param("o") OptLogBean optLogBean, @Param("s") StaffBean staffBean);

    /**
     * 分页查询所有操做日志
     *
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = int.class),
            @Result(property = "optLogNo", column = "opt_log_no", javaType = String.class),
            @Result(property = "logTime", column = "log_time", javaType = Date.class),
            @Result(property = "ip", column = "ip", javaType = String.class),
            @Result(property = "logContent",column = "log_content",javaType = String.class),
            @Result(property = "staffBean", column = "stuff_id", javaType = StaffBean.class,one =
            @One(select = "com.woniuxy.stage3demo.mapper.chenyuxuanMapper.StaffMapper.findStaffById", fetchType = FetchType.LAZY)),
            @Result(property = "delFlag", column = "del_flag", javaType = String.class)
    })
    @Select("<script>" +
            "select id,opt_log_no,log_time,ip,log_content,stuff_id from opt_log " +
            "<where> " +
            "del_flag=0 " +
            "<if test='o.staffBean !=null'>" +
            "and stuff_id = #{o.staffBean.id} " +
            "</if> " +
            "<if test='o.logTime !=null'>" +
            "and log_time like concat(#{o.logTime},'%') " +
            "</if>" +
            "<if test='p.sort != null and p.sortOrder != null'>" +
            "order by ${p.sort} ${p.sortOrder}" +
            "</if>" +
            "</where>" +
            "</script>")
    List<OptLogBean> findAllOptLog(@Param("p") PageRequest pageRequest, @Param("o") OptLogBean optLogBean);

    /**
     * 根据员工ID查询操作日志
     *
     * @param id
     * @return
     */
    OptLogBean findOneOptLog(int id);

    /**
     * 修改日志
     * @param optLogBean
     * @return
     */
    @Update("<script>" +
            "update opt_log" +
            "<set>" +
            "<if test='o.optLogNo != null'>" +
            " opt_log_no = #{o.optLogNo}," +
            "</if>"+
            "<if test='o.logTime != null'>" +
            " log_time = #{o.logTime}," +
            "</if>"+
            "<if test='o.ip != null'>" +
            " ip = #{o.ip}," +
            "</if>"+
            "<if test='o.logContent != null'>" +
            " log_content = #{o.logContent}," +
            "</if>"+
            "<if test='o.staffBean.id != null'>" +
            " stuff_id = #{o.staffBean.id}," +
            "</if>"+
            "del_flag='0'"+
            "where id = #{o.id}" +
            "</set>"+
            "</script>")
    int updateOptLog(@Param("o") OptLogBean optLogBean);


    /**
     * 删除日志改变删除标记del_flag变为1
     * @param id
     */
    @Update("update opt_log set del_flag = 1 where id = #{id}")
    int deleteOpt(@Param("id") int id);
}
